 <template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-button class="filter-item" v-if="methodManager_btn_add" style="margin-left: 10px;" @click="handleCreate" type="primary" icon="plus">添加</el-button>
    </div>
    <el-table :key='tableKey' :data="list" v-loading.body="listLoading" border fit highlight-current-row style="width: 100%">
      <el-table-column align="center" label="id" width="60">
        <template scope="scope">
          <span>{{scope.row.id}}</span>
        </template>
      </el-table-column>
      <el-table-column width="200px" align="center" label="场景代码">
      <template scope="scope">
        <span>{{scope.row.sceneCode}}</span>
      </template>
      </el-table-column>
      <el-table-column width="200px" align="center" label="支付方式代码">
        <template scope="scope">
          <span>{{scope.row.methodCode}}</span>
        </template>
      </el-table-column>
      <el-table-column width="300px" align="center" label="支付方式">
        <template scope="scope">
          <span>{{scope.row.method}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="250"> 
          <template scope="scope">
            <el-button size="small" v-if="methodManager_btn_del"  type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
      </el-table-column>
    </el-table>
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
    </div>

    <el-dialog title="添加" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="支付方式" prop="method">
          <el-select v-model="form.method" placeholder="请选择" clearable="">
              <el-option v-for="item in methodList" :key="item.code" :label="item.value+' '+item.remark" :value="item.code+'&'+item.value">
              </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="create('form')">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { methodAll, add, deleteObj } from '@/api/crm/scene/payMethod'
import { listWithType } from '@/api/admin/dict/index'
import { mapGetters } from 'vuex'
export default {
  name: 'functionList',
  data() {
    return {
      sceneCode: '',
      methodList: [],
      form: {
        methodCode: '',
        method: '',
        sceneCode: ''
      },
      rules: {
        method: [
          {
            required: true,
            message: '不能为空',
            trigger: 'blur'
          }
        ]
      },
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        sceneCode: ''
      },
      listQueryMethod: {
        type: 'payMethod'
      },
      dialogFormVisible: false,
      methodManager_btn_add: false,
      methodManager_btn_del: false,
      tableKey: 0
    }
  },
  created() {
    this.sceneCode = this.$route.query.row.sceneCode
    this.listQuery.sceneCode = this.sceneCode
    this.getList()
    this.methodManager_btn_add = this.elements['methodManager:btn_add']
    this.methodManager_btn_del = this.elements['methodManager:btn_del']
  },
  computed: {
    ...mapGetters(['elements', 'platformId'])
  },
  methods: {
    getList() {
      this.listLoading = true
      methodAll(this.listQuery).then(response => {
        this.list = response.data.rows
        this.total = response.data.total
        this.listLoading = false
      })
    },
    getMethods() {
      listWithType(this.listQueryMethod)
        .then(response => {
          this.methodList = response.dictList
        })
    },
    handleFilter() {
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    handleCreate() {
      this.getMethods()
      this.resetTemp()
      this.dialogFormVisible = true
    },
    create(formName) {
      var method = this.form.method
      this.form.methodCode = method.split('&')[0]
      this.form.method = method.split('&')[1]
      this.form.sceneCode = this.sceneCode
      const set = this.$refs
      set[formName].validate(valid => {
        if (valid) {
          add(this.form).then(response => {
            if (response.code === '0000') {
              this.dialogFormVisible = false
              this.getList()
              this.$notify({
                title: '成功',
                message: '创建成功',
                type: 'success',
                duration: 2000
              })
            } else {
              alert('已经存在')
            }
          })
        } else {
          return false
        }
      })
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteObj(row).then(() => {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
          this.getList()
        })
      })
    },
    resetTemp() {
      this.form = {
        methodCode: '',
        method: '',
        sceneCode: ''
      }
    }
  }
}
</script>
